package com.crazymakercircle.entity;

import com.crazymakercircle.constants.ServerConstants;
import com.crazymakercircle.util.JsonUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

/**
 * 工作节点的POJO类
 * create by 尼恩 @ 疯狂创客圈
 **/
@Data
public class TestResultData implements Serializable
{
    private static final long serialVersionUID = -499010884211304846L;


    private String path = "un-known";


    //任务名称
    private String name = "un-known";

    private String start_time;
    private String end_time;
    private int thread_num;
    private int client_num;
    private double test_time;
    private long total_req;
    private long total_res;
    private long total_err;
    private String qps;
    private String avg_latency;
    private long below_10;
    private long between_10_20;
    private long between_20_30;
    private long over_30;
    private long takeTime;


    public TestResultData()
    {
    }

    public TestResultData(String name)
    {
        this.name = name;
    }

    public TestResultData(String start_time, String end_time, int thread_num,
                          int client_num, double test_time, long total_req, long total_res, long total_err, String qps, String avg_latency, long below_10, long between_10_20, long between_20_30, long over_30)
    {
        this.start_time = start_time;
        this.end_time = end_time;
        this.thread_num = thread_num;
        this.client_num = client_num;
        this.test_time = test_time;
        this.total_req = total_req;
        this.total_res = total_res;
        this.total_err = total_err;
        this.qps = qps;
        this.avg_latency = avg_latency;
        this.below_10 = below_10;
        this.between_10_20 = between_10_20;
        this.between_20_30 = between_20_30;
        this.over_30 = over_30;
    }

    @Override
    public String toString()
    {
        return "WorkerData{name='" + name + '\'' + '}';
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TestResultData node = (TestResultData) o;
//        return id == node.id &&
        return Objects.equals(name, node.name);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(name);
    }

    public static long getIdByPath(String path)
    {
        String sid = null;
        if (null == path)
        {
            throw new RuntimeException("节点路径有误");
        }
        int index = path.lastIndexOf(ServerConstants.PATH_TASK_INST);
        if (index >= 0)
        {
            index += ServerConstants.PATH_TASK_INST.length();
            sid = index <= path.length() ? path.substring(index) : null;
        }

        if (null == sid)
        {
            throw new RuntimeException("节点ID获取失败");
        }

        return Long.parseLong(sid);

    }

    public String toJson()
    {
        return JsonUtil.pojoToJson(this);
    }

    public static TestResultData fromJson(byte[] jsonBytes)
    {
        return JsonUtil.jsonBytes2Object(jsonBytes, TestResultData.class);
    }

    public static TestResultData fromJson(String json)
    {
        return JsonUtil.jsonToPojo(json, TestResultData.class);
    }


    public void report()
    {
        System.out.println("-------------------------------------- TEST REPORT BEGIN------------------------------------------------");
        System.out.println("| START_TIME            : " + this.getStart_time());
        System.out.println("| END_TIME              : " + this.getEnd_time());
        System.out.println("| TAKE_TIME              : " + this.getTakeTime());
        System.out.println("| THREAD_NUM            : " + this.getThread_num());
        System.out.println("| CLIENT_NUM            : " + this.getClient_num());
        System.out.println("| TEST_TIME             : " + this.getTest_time() + " min");
        System.out.println("| TOTAL_REQ             : " + this.getTotal_req());
        System.out.println("| TOTAL_RES             : " + this.getTotal_res());
        System.out.println("| TOTAL_ERR             : " + this.getTotal_err());
        System.out.println("| BELOW_10              : " + this.getBelow_10());
        System.out.println("| BT_10_20              : " + this.getBetween_10_20());
        System.out.println("| BT_20_30              : " + this.getBetween_20_30());
        System.out.println("| OVER_30               : " + this.getOver_30());
        System.out.println("| AVG_LATENCY           : " + this.getAvg_latency() + " ms");
        System.out.println("| QPS                   : " + this.getQps());
        System.out.println("-------------------------------------- TEST REPORT END------------------------------------------------");
    }
}